package com.zenithmind.version.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zenithmind.version.pojo.domain.Repository;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * 仓库Mapper接口
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Mapper
public interface RepositoryMapper extends BaseMapper<Repository> {

    /**
     * 分页查询仓库列表
     * 
     * @param page 分页参数
     * @param ownerId 所有者ID
     * @param type 仓库类型
     * @param status 仓库状态
     * @param keyword 关键词
     * @return 仓库列表
     */
    IPage<Repository> selectRepositoryPage(Page<Repository> page,
                                          @Param("ownerId") String ownerId,
                                          @Param("type") Integer type,
                                          @Param("status") Integer status,
                                          @Param("keyword") String keyword);

    /**
     * 查询用户的仓库列表
     * 
     * @param userId 用户ID
     * @param type 仓库类型
     * @return 仓库列表
     */
    List<Repository> selectUserRepositories(@Param("userId") String userId,
                                           @Param("type") Integer type);

    /**
     * 查询公开仓库列表
     * 
     * @param keyword 关键词
     * @param language 编程语言
     * @param limit 限制数量
     * @return 仓库列表
     */
    List<Repository> selectPublicRepositories(@Param("keyword") String keyword,
                                             @Param("language") String language,
                                             @Param("limit") Integer limit);

    /**
     * 根据名称和所有者查询仓库
     * 
     * @param name 仓库名称
     * @param ownerId 所有者ID
     * @return 仓库
     */
    Repository selectByNameAndOwner(@Param("name") String name,
                                   @Param("ownerId") String ownerId);

    /**
     * 更新仓库统计信息
     * 
     * @param repositoryId 仓库ID
     * @param size 仓库大小
     * @param fileCount 文件数量
     * @param commitCount 提交数量
     * @param branchCount 分支数量
     * @param tagCount 标签数量
     * @return 更新行数
     */
    int updateStatistics(@Param("repositoryId") String repositoryId,
                        @Param("size") Long size,
                        @Param("fileCount") Integer fileCount,
                        @Param("commitCount") Integer commitCount,
                        @Param("branchCount") Integer branchCount,
                        @Param("tagCount") Integer tagCount);

    /**
     * 增加贡献者数量
     * 
     * @param repositoryId 仓库ID
     * @param count 增加数量
     * @return 更新行数
     */
    int increaseContributorCount(@Param("repositoryId") String repositoryId,
                                @Param("count") Integer count);

    /**
     * 更新最后活动时间
     * 
     * @param repositoryId 仓库ID
     * @return 更新行数
     */
    int updateLastActivityTime(@Param("repositoryId") String repositoryId);

    /**
     * 查询仓库统计信息
     * 
     * @param ownerId 所有者ID
     * @return 统计信息
     */
    RepositoryStatistics selectStatistics(@Param("ownerId") String ownerId);

    /**
     * 仓库统计信息
     */
    class RepositoryStatistics {
        private Long totalRepositories;
        private Long publicRepositories;
        private Long privateRepositories;
        private Long archivedRepositories;
        private Long totalSize;
        private Long totalCommits;
        private Long totalBranches;
        private Long totalTags;

        // Getters and Setters
        public Long getTotalRepositories() { return totalRepositories; }
        public void setTotalRepositories(Long totalRepositories) { this.totalRepositories = totalRepositories; }
        
        public Long getPublicRepositories() { return publicRepositories; }
        public void setPublicRepositories(Long publicRepositories) { this.publicRepositories = publicRepositories; }
        
        public Long getPrivateRepositories() { return privateRepositories; }
        public void setPrivateRepositories(Long privateRepositories) { this.privateRepositories = privateRepositories; }
        
        public Long getArchivedRepositories() { return archivedRepositories; }
        public void setArchivedRepositories(Long archivedRepositories) { this.archivedRepositories = archivedRepositories; }
        
        public Long getTotalSize() { return totalSize; }
        public void setTotalSize(Long totalSize) { this.totalSize = totalSize; }
        
        public Long getTotalCommits() { return totalCommits; }
        public void setTotalCommits(Long totalCommits) { this.totalCommits = totalCommits; }
        
        public Long getTotalBranches() { return totalBranches; }
        public void setTotalBranches(Long totalBranches) { this.totalBranches = totalBranches; }
        
        public Long getTotalTags() { return totalTags; }
        public void setTotalTags(Long totalTags) { this.totalTags = totalTags; }
    }
}
